package com.yuyou.fn.tradingservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.tradingservice.entity.ServiceImage;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.mapper.ServiceImageMapper;
import com.yuyou.fn.tradingservice.service.IServiceImageService;
import com.yuyou.fn.tradingservice.service.IServiceProductService;
import com.yuyou.fn.tradingservice.vo.ServiceProductVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * ServiceImage 表数据服务层接口实现类
 *
 */
@Service
public class ServiceImageServiceImpl extends SuperServiceImpl<ServiceImageMapper, ServiceImage,Long> implements IServiceImageService {

    @Resource
    private IServiceProductService productService;

    @Override
    public Long saveOrUpdateImage(ServiceImage image) {
        if (image.getImageId()==null){
            image.setCreateTime(TimeUtils.now());
            image.setUpdateTime(TimeUtils.now());
            insertSelective(image);
        }else {
            image.setUpdateTime(TimeUtils.now());
            updateSelectiveById(image);
        }
        return image.getImageId();

    }

    @Override
    public void insertUseId(Long imageId,Integer type,Long id) {
        ServiceImage image = findById(imageId);
        image.setUseId(id);
        image.setType(type);
        updateSelectiveById(image);
    }

    @Override
    public List<ServiceImage> findImageByUseId(Long useId) {
        Example<ServiceImage> example = ServiceImage.createExample();
        example.orderBy("updateTime").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("useId",useId);
        return findListByExample(example);
    }

    /**
     * 根据商品id获取第一张图片
     *
     * @param productId
     * @return
     */
    @Override
    public ServiceImage findFirstImage(Long productId) {
        Example<ServiceImage> example = ServiceImage.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        ServiceImage serviceImage = findOneByExample(example);
        return serviceImage;
    }


    @Override
    public void generateProductImages(ServiceProduct serviceProduct, String productImages) {

        if (productImages==null) throw new BusException("图片数组json为空");
        Long productId = serviceProduct.getProductId();
        Integer type = serviceProduct.getType();

        Example<ServiceImage> example = ServiceImage.createExample();
        example.createCriteria().andEqualTo("productId",productId);
        deleteByExample(example);

        List<String> imageUrls = JSON.parseObject(productImages, new TypeReference<List<String>>() {});
        if (imageUrls.size()==0) return;

        serviceProduct.setImageUrl(imageUrls.get(0));
        productService.updateSelectiveById(serviceProduct);

        ArrayList<ServiceImage> list = new ArrayList<>();
        for (String imageUrl : imageUrls) {
            ServiceImage serviceImage = new ServiceImage();
            serviceImage.setProductId(productId);
            serviceImage.setImageUrl(imageUrl);
            serviceImage.setType(type);
            serviceImage.setCreateTime(TimeUtils.now());
            serviceImage.setUpdateTime(TimeUtils.now());
            list.add(serviceImage);
        }
        insertBatch(list);
    }

    @Override
    public String[] findUrlArrayByProductId(Long productId) {
        ArrayList<String> list = new ArrayList<>();
        Example<ServiceImage> example = ServiceImage.createExample();
        example.createCriteria().andEqualTo("productId",productId);
        List<ServiceImage> serviceImages = findListByExample(example);
        for (ServiceImage serviceImage : serviceImages) {
            list.add(serviceImage.getImageUrl());
        }
        return list.toArray(new String[list.size()]);
    }
}